﻿using System.Drawing;
using System.Windows.Forms;

namespace NetTrafficStatistics
{
	class DataPanel : Panel
	{
		private DataTable DataTable;

		public DataPanel ()
		{
			SetStyle (ControlStyles.UserPaint, true);
		}

		protected override void OnPaint (PaintEventArgs e)
		{
			var g = e.Graphics;
			Pen pen = new Pen (Color.Gray);
			Brush brush = new SolidBrush (Color.LightGray);

			g.FillRectangles (brush, new[] { new Rectangle (0, 0, Width - 1, Tools.Header), new Rectangle (0, Height - Tools.Footer - Tools.Header - 1, Width - 1, Tools.Header) });
			g.DrawRectangle (pen, 0, 0, Width - 1, Height - Tools.Footer - 1);

			g.DrawLine (pen, 0, Tools.Header, Width - 1, Tools.Header);
			g.DrawLine (pen, 0, Height - Tools.Footer - Tools.Header - 1, Width - 1, Height - Tools.Footer - Tools.Header - 1);
			g.DrawLine (pen, Tools.DateWidth, 0, Tools.DateWidth, Tools.Header);
			g.DrawLine (pen, Tools.DateWidth + Tools.DataWidth, 0, Tools.DateWidth + Tools.DataWidth, Tools.Header);

			pen.Dispose ();
			g.Dispose ();
		}

		public bool ShowTable (Params p, Tools.Units u)
		{
			if (DataTable != null) DataTable.Dispose ();
			if (p.Custom) return false;
			var arr = SqLiteDb.ReadTable (p);
			if ((arr == null) || (arr.Count != 3)) return false;

			DataTable = new DataTable (arr, u, p.HeaderType)
			{
				Top = Tools.Header,
				Width = Width - 2,
				Left = 1,
				BackColor = Color.Transparent
			};
			Height = DataTable.Height + Tools.Header + Tools.Footer + 2;
			Controls.Add (DataTable);
			return true;
		}
	}
}
